<?php

namespace plugin\muadmin\app\admin\controller\auth;

use plugin\muadmin\app\admin\dict\MenuTypeDict;
use plugin\muadmin\app\common\base\BaseAdminController;
use plugin\muadmin\app\common\model\AuthRule;
use plugin\muadmin\app\common\model\Plugin;

class Rule extends BaseAdminController
{
    public function initialize()
    {
        parent::initialize();
        $this->model = (new AuthRule());
        //  菜单类型
        $this->assignConfig('menu_type_list', MenuTypeDict::getInstance()->getOptions());
        //  菜单列表
        $list = AuthRule::where('ismenu',1)
            ->whereIn('type',['dir','menu'])
            ->order('id asc,weigh desc')
            ->select()->toArray();
        if(!empty($list)){
            $list = list_to_tree($list, $this->model->getPk(), $this->pidName, $this->childrenName);
        }
        $list = array_merge([['id'=>0,'title'=>__('最上级'),'type'=>'dir']],$list);
        $this->assignConfig('menu_list', $list);
        //插件列表
        $pluginList = Plugin::where('status',1)->select()->toArray();
        $this->assignConfig('plugin_list', $pluginList);
    }
    public function index()
    {
        if($this->request->isAjax()){
            list($where, $sort, $order, $page, $limit) = $this->buildParameter();
            $list = $this->model->where($where)
                ->order('id asc,weigh desc')
                ->select()->toArray();
            if (!empty($list)) {
                foreach ($list as &$v){
                    $v['title'] = __($v['title']);
                }
                unset($v);
                $list = list_to_tree($list, $this->model->getPk(), $this->pidName, $this->childrenName);
            }
            $res = ['total' => count($list), 'rows' => $list];
            return $this->success('ok',$res);
        }
        return parent::index();
    }
    public function del()
    {
        $data = $this->request->all();
        if(empty($data['ids'])){
            return $this->error('请选择删除项');
        }
        $ids = $data['ids'];
        if(is_string($ids)){
            $ids = explode(',',$ids);
        }
        $selects = $this->model->getAllIds($ids);
        if(!empty($selects)){
            $this->model->destroy($selects);
        }
        return $this->success('删除成功');
    }

    /**
     * 获取模块
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_module()
    {
        $ids = $this->param('plugin');
        if(empty($ids)){
            return $this->error('请选择插件');
        }
        $plugin = Plugin::where('key',$ids)->find();
        if(empty($plugin)){
            return $this->error('插件不存在');
        }
        $modules = get_plugin_module($plugin['key']);
        $nv = [];
        foreach ($modules as $v){
            $nv[] = [
                'value'=>$v,
                'label'=>ucfirst($v)
            ];
        }
        return $this->success('ok',$nv);
    }
}